import 'package:cook_dinner/config/include.dart';
import 'package:cook_dinner/pages/dishes/dishes.dart';
import 'package:cook_dinner/pages/home/home.dart';
import 'package:flutter/material.dart';

class IndexPage extends StatefulWidget {
  const IndexPage({Key? key}) : super(key: key);

  @override
  State<IndexPage> createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  int _selectedIndex = 0;
  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  static const pages = [
    AppPage.home,
    HomePage(),
    AppPage.order,
    AppPage.user,
    AppPage.createCook,
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: pages[_selectedIndex],
      bottomNavigationBar: BottomAppBar(
        color: Colors.white,
        shape: const CircularNotchedRectangle(), // 创建凹槽以适应FAB
        notchMargin: 4.0, // 凹槽边缘的间距
        child: Container(
          height: 75.rpx,
          padding: EdgeInsets.only(bottom: 15.rpx),
          child: Row(
            // 自定义底部导航栏项
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              _navButton(
                index: 0,
                title: '首页',
                icon: Icons.home_rounded,
                onTap: () => _onItemTapped(0),
              ),
              _navButton(
                index: 1,
                title: '社区',
                icon: Icons.local_dining,
                onTap: () => _onItemTapped(1),
              ),
              _navButton(
                title: '',
                onTap: null,
              ),
              _navButton(
                index: 2,
                title: '商城',
                icon: Icons.shopping_cart,
                onTap: () => _onItemTapped(2),
              ),
              _navButton(
                index: 3,
                title: '我的',
                icon: Icons.local_library,
                onTap: () => _onItemTapped(3),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _onItemTapped(4), // 设置图标
        backgroundColor: const Color(0xfffbc508), // 添加点击事件
        child: Icon(Icons.add, size: 40.rpx), // 设置背景色
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }

  Widget _navButton({onTap, title, icon, index}) {
    return InkWell(
      onTap: onTap,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          Icon(
            icon ?? Icons.add,
            size: 30.rpx,
            color: icon == null
                ? Colors.transparent
                : _selectedIndex == index
                    ? const Color(0xffFCC302)
                    : const Color(0xff999999),
          ),
          Text(
            title,
            style: TextStyle(
                fontSize: 12.rpx,
                color: _selectedIndex == index
                    ? const Color(0xffFCC302)
                    : const Color(0xff999999)),
          )
        ],
      ),
    );
  }
}
